package com.attendance.www.hr.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.attendance.www.hr.beans.CorporationBean;
import com.attendance.www.hr.beans.EmployeeBean;
import com.attendance.www.hr.beans.EmployeeCorporationBean;
import com.attendance.www.hr.beans.EmployeePageBean;
import com.attendance.www.hr.dao.mapper.CorporationMapper;
import com.attendance.www.hr.dao.mapper.EmployeeCorporationExtMapper;
import com.attendance.www.hr.dao.mapper.EmployeeCorporationMapper;
import com.attendance.www.hr.dao.mapper.EmployeeExtMapper;
import com.attendance.www.hr.entitys.Corporation;
import com.attendance.www.hr.entitys.EmployeeCorporation;
import com.attendance.www.hr.entitys.EmployeeCorporationExample;
import com.attendance.www.hr.exception.ResultFaultException;
import com.attendance.www.hr.service.IEmployeeCorporationService;

@Service
public class EmployeeCorporationServiceImpl implements IEmployeeCorporationService {

	@Resource
	private EmployeeCorporationExtMapper employeeCorporationExtDao;
	@Resource
	private EmployeeCorporationMapper employeeCorporationDao;
	@Resource
	private CorporationMapper corporationDao;
	@Resource
	private EmployeeExtMapper employeeExtDao;

	/**
	 * 修改HR的公司，新增和修改、删除共用的方法
	 */
	@Override
	public boolean updateEmployeeCorporation(EmployeeCorporationBean employeeCorporationBean) {
		EmployeeCorporation employeeCorporation = new EmployeeCorporation();
		Integer employeeId = employeeCorporationBean.getEmployeeId();
		employeeCorporation.setEmployeeId(employeeId);
		EmployeeCorporationExample example = new EmployeeCorporationExample();
		example.createCriteria()
		.andIsCorporationEqualTo(1).
		andEmployeeIdEqualTo(employeeId);
		if(employeeCorporationBean.getType()==1) {
			if(employeeCorporationDao.selectByExample(example).size()>0) {
				throw new ResultFaultException("0_00010");
			};
		}
		// 清空 0是默认公司，1是分配公司

		int record = employeeCorporationDao.deleteByExample(example);
		// 添加
		List<CorporationBean> corporationBeanList = employeeCorporationBean.getCorporationBeanList();
		for (CorporationBean corporationBean : corporationBeanList) {
			employeeCorporation.setCorporationId(corporationBean.getCorporationId());
			employeeCorporation.setIsCorporation(1);//此处1是分配的公司
			employeeCorporation.setIsApproval(corporationBean.getIsApproval());
			employeeCorporation.setFromDate(new Date());
			record = employeeCorporationDao.insertSelective(employeeCorporation);
			if (record <= 0) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 查询员工公司分配信息
	 */
	@Override
	public EmployeePageBean getEmployeeCorporationList(EmployeeBean employeeBean, Integer startNum, Integer pageCount) {
		// 查询人员集合
		List<EmployeeBean> employeeBeanList = employeeCorporationExtDao.getEmployeeCorporationPageList(employeeBean, startNum, pageCount);
		List<EmployeeBean> newList = new ArrayList<EmployeeBean>();
		// 导入员工的公司List

		for (EmployeeBean employeeBeanPage : employeeBeanList) {
			Integer employeeId = employeeBeanPage.getEmployeeId();
			// 根据员工ID查 员工公司类集合
			EmployeeCorporationExample example = new EmployeeCorporationExample();
			example.createCriteria().andEmployeeIdEqualTo(employeeId).andIsCorporationEqualTo(1);
			example.setOrderByClause("from_date desc");
			List<EmployeeCorporation> employeeCorporationList = employeeCorporationDao.selectByExample(example);

			List<CorporationBean> corporationBeanList = new ArrayList<>();
			for (EmployeeCorporation employeeCorporation : employeeCorporationList) {
				// 根据公司ID查询公司信息
				Integer corporationId = employeeCorporation.getCorporationId();
				Corporation corporation = corporationDao.selectByPrimaryKey(corporationId);
				if (corporation.getIsDelete() != 1) {
					// 导入公司bean类
					CorporationBean corporationBean = new CorporationBean();
					corporationBean.setCorporationId(corporationId);
					corporationBean.setCorporationCode(corporation.getCorporationCode());
					corporationBean.setCorporationName(corporation.getCorporationName());
					corporationBean.setDescription(corporation.getDescription());
					corporationBean.setIsDelete(corporation.getIsDelete());
					corporationBean.setIsApproval(employeeCorporation.getIsApproval());
					corporationBean.setFromDate(employeeCorporation.getFromDate());
					corporationBeanList.add(corporationBean);
				}
			}
			employeeBeanPage.setCorporationBeanList(corporationBeanList);
			newList.add(employeeBeanPage);
		}
		EmployeePageBean epb = new EmployeePageBean();
		epb.setEmployeeBeanList(newList);
		epb.setPageTotal(employeeCorporationExtDao.getEmployeeCorporationSum(employeeBean));
		return epb;
	}

	@Override
	public List<EmployeeCorporation> getEmployeeCorporationListById(String employeeId) {
		EmployeeCorporationExample example = new EmployeeCorporationExample();
		example.createCriteria().andEmployeeIdEqualTo(Integer.parseInt(employeeId));
		List<EmployeeCorporation> list = employeeCorporationDao.selectByExample(example);
		return list;
	}

	@Override
	public List<EmployeeCorporation> getEmployeeCorporationListByCorporationId(Integer corporationId) {
		EmployeeCorporationExample example = new EmployeeCorporationExample();
		example.createCriteria().andCorporationIdEqualTo(corporationId);
		List<EmployeeCorporation> list = employeeCorporationDao.selectByExample(example);
		return list;
	}

}
